package com.hunan.api.catv.service.biz.album;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hunan.api.catv.common.CacheName;
import com.hunan.api.catv.entity.ServiceResult;
import com.hunan.api.catv.entity.enums.State;
import com.hunan.api.catv.entity.po.AlbumVideo;
import com.hunan.api.catv.entity.vo.AlbumVideoVo;
import com.hunan.api.catv.service.ResultService;
import com.hunan.api.catv.service.biz.cache.CacheBiz;
import com.hunan.api.catv.service.biz.tencent_cloud.im.ImBiz;
import com.hunan.api.catv.service.impl.album.AlbumVideoService;
import com.hunan.api.catv.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.util.List;

@CacheConfig(cacheNames = CacheName.albumVideo)
@Service
public class AlbumVideoBiz extends ResultService {

    @Autowired
    AlbumVideoBiz albumVideoBiz;

    @Autowired
    AlbumVideoService albumVideoService;

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    ImBiz imBiz;

    @Autowired
    CacheBiz cacheBiz;

    public ServiceResult add(AlbumVideo albumVideo) {
        Integer openChat = albumVideo.getOpenChat();
        if (1 == openChat) {
            ServiceResult creatGroupResult = imBiz.createGroup(albumVideo.getTitle());
            if (!creatGroupResult.getStatus()) {
                return creatGroupResult;
            }
            albumVideo.setGroupId(creatGroupResult.getData().toString());
        }
        return albumVideoService.add(albumVideo);
    }

    public ServiceResult update(AlbumVideo albumVideo) {
        AlbumVideoVo albumVideoVo = albumVideoService.getById(albumVideo.getVideoId());
        if (1 == albumVideoVo.getOpenChat() && 0 == albumVideo.getOpenChat()) {
            ServiceResult destoryResult = imBiz.destoryGroup(albumVideoVo.getGroupId());
            if (!destoryResult.getStatus()) {
                return destoryResult;
            }
            albumVideo.setGroupId("");
        }
        if (0 == albumVideoVo.getOpenChat() && 1 == albumVideo.getOpenChat()) {
            ServiceResult createResult = imBiz.createGroup(albumVideo.getTitle());
            if (!createResult.getStatus()) {
                return createResult;
            }
            albumVideo.setGroupId(createResult.getData().toString());
        }
        ServiceResult serviceResult = albumVideoService.update(albumVideo);
        if (serviceResult.getStatus()) {
            String albumVideoKey = MessageFormat.format(CacheName.albumVideoList, String.valueOf(albumVideo.getVideoId()));
            cacheBiz.flushCache("albumVideoListCache?" + albumVideo.getVideoId(), albumVideoKey);
        }
        return serviceResult;
    }

    public ServiceResult update(Integer videoId, Integer state) {
        AlbumVideo albumVideo = new AlbumVideo();
        albumVideo.setVideoId(videoId);
        albumVideo.setState(state);
        ServiceResult serviceResult = albumVideoService.update(albumVideo);
        if (serviceResult.getStatus()) {
            String albumVideoKey = MessageFormat.format(CacheName.albumVideoList, String.valueOf(videoId));
            cacheBiz.flushCache("albumVideoListCache?" + videoId, albumVideoKey);
        }
        return serviceResult;
    }

    public AlbumVideoVo info(Integer videoId) {
        AlbumVideoVo albumVideoVo = albumVideoService.getById(videoId);
        State state = State.getResult(albumVideoVo.getState());
        albumVideoVo.setStateMsg(state.getMsg());
        return albumVideoVo;
    }

    public PageInfo<AlbumVideoVo> list(int pageNum, int pageSize, Integer albumId) {
        PageHelper.startPage(pageNum, pageSize);
        AlbumVideo albumVideo = new AlbumVideo();
        albumVideo.setAlbumId(albumId);
        List<AlbumVideoVo> list = albumVideoService.list(albumVideo);
        for (int i = 0; i < list.size(); i++) {
            AlbumVideoVo albumVideoVo = list.get(i);
            State state = State.getResult(albumVideoVo.getState());
            albumVideoVo.setStateMsg(state.getMsg());
        }
        return new PageInfo<>(list);
    }


    @Cacheable(unless = "#result == null")
    public List<AlbumVideoVo> list(Integer albumId) {
        AlbumVideo albumVideo = new AlbumVideo();
        albumVideo.setAlbumId(albumId);
        albumVideo.setState(1);
        return albumVideoService.simpleList(albumVideo);
    }

}
